package com.ruoyi.project.mall.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.PageUtils;
import com.ruoyi.common.utils.bootdo.Query;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.domain.MallInvoiceShopDO;
import com.ruoyi.project.mall.service.MallInvoiceShopService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商家开票规则
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-12-25 10:18:47
 */
 
@Controller
@RequestMapping("/mall/mallInvoiceShop")
public class MallInvoiceShopController {
	@Autowired
	private MallInvoiceShopService mallInvoiceShopService;
	
	@GetMapping()
	@RequiresPermissions("mall:mallInvoiceShop:mallInvoiceShop")
	String MallInvoiceShop(){
	    return "mall/mallInvoiceShop/mallInvoiceShop";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("mall:mallInvoiceShop:mallInvoiceShop")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&& StringUtils.isNotBlank(params.get("name").toString())){
            List<MallInvoiceShopDO> mallInvoiceShopList = mallInvoiceShopService.likeList(query);
            int total = mallInvoiceShopService.likeListCount(query);
            PageUtils pageUtils = new PageUtils(mallInvoiceShopList, total);
            return pageUtils;
		}

		List<MallInvoiceShopDO> mallInvoiceShopList = mallInvoiceShopService.list(query);
		int total = mallInvoiceShopService.count(query);
		PageUtils pageUtils = new PageUtils(mallInvoiceShopList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("mall:mallInvoiceShop:add")
	String add(){
	    return "mall/mallInvoiceShop/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("mall:mallInvoiceShop:edit")
	String edit(@PathVariable("id") String id,Model model){
		MallInvoiceShopDO mallInvoiceShop = mallInvoiceShopService.get(id);
		model.addAttribute("mallInvoiceShop", mallInvoiceShop);
	    return "mall/mallInvoiceShop/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallInvoiceShop:add")
	public R save(MallInvoiceShopDO mallInvoiceShop){
	    mallInvoiceShop.setId(UuidUtil.get32UUID());
	    mallInvoiceShop.setDelFlag(0);
	    mallInvoiceShop.setCaAt(new Date());
		if(mallInvoiceShopService.save(mallInvoiceShop)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("mall:mallInvoiceShop:edit")
	public R update( MallInvoiceShopDO mallInvoiceShop){
		mallInvoiceShop.setUpAt(new Date());
		mallInvoiceShopService.update(mallInvoiceShop);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:mallInvoiceShop:remove")
	public R remove( String id){
		MallInvoiceShopDO mallInvoiceShop = mallInvoiceShopService.get(id);
		mallInvoiceShop.setDelFlag(1);
		mallInvoiceShop.setUpAt(new Date());
		if(mallInvoiceShopService.update(mallInvoiceShop)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:mallInvoiceShop:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MallInvoiceShopDO mallInvoiceShop = mallInvoiceShopService.get(string);
				mallInvoiceShop.setDelFlag(1);
				mallInvoiceShop.setUpAt(new Date());
				mallInvoiceShopService.update(mallInvoiceShop);
			}
		}
	
		return R.ok();
	}
	
}
